package yu.com.time.calclator

import yu.com.time.unit.DateTimeUnit
import yu.com.time.unit.DateTimeUnit.*
import java.time.LocalDateTime
import java.time.temporal.ChronoUnit


/**
 * jdk8日期时间计算器
 * created zpf on 2018/10/23 0023
 * @param origTime 原始时间
 * @param unit     时间计算单位
 */
open class LocalDateTimeCalculator(val origTime: LocalDateTime, val unit: DateTimeUnit) : Calculator<LocalDateTime, LocalDateTime> {

    override fun plus(addition: Long): LocalDateTime {
        return when (unit) {
            YEAR -> origTime.plusYears(addition)
            MONTH -> origTime.plusMonths(addition)
            WEEK -> origTime.plusWeeks(addition)
            DAY -> origTime.plusDays(addition)
            HOUR -> origTime.plusHours(addition)
            MINUTE -> origTime.plusMinutes(addition)
            SECOND -> origTime.plusSeconds(addition)
            MILLI -> origTime.plusNanos(addition * 1000 * 1000)
            NANO -> origTime.plusNanos(addition)
        }
    }

    override fun minus(subtraction: LocalDateTime): Long {
        return when (unit) {
            YEAR -> subtraction.until(origTime, ChronoUnit.YEARS)
            MONTH -> subtraction.until(origTime, ChronoUnit.MONTHS)
            WEEK -> subtraction.until(origTime, ChronoUnit.WEEKS)
            DAY -> subtraction.until(origTime, ChronoUnit.DAYS)
            HOUR -> subtraction.until(origTime, ChronoUnit.HOURS)
            MINUTE -> subtraction.until(origTime, ChronoUnit.MINUTES)
            SECOND -> subtraction.until(origTime, ChronoUnit.SECONDS)
            MILLI -> subtraction.until(origTime, ChronoUnit.MILLIS)
            NANO -> subtraction.until(origTime, ChronoUnit.NANOS)
        }
    }
}